import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/risk_management_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/inventory_system/daily_inspection_checklist/daily_inspection_checklist_controller.dart';

///日常巡检清单
class DailyInspectionChecklistPage extends StatelessWidget {
  DailyInspectionChecklistPage({super.key}) {
    Get.lazyPut(DailyInspectionChecklistController.new);
  }

  @override
  Widget build(BuildContext context) {
    final DailyInspectionChecklistController controller = Get.find<DailyInspectionChecklistController>();
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(controller),
                      ),
                      AppGaps.hGap8,
                      _screen(controller),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<RiskManagementListEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.dailyInspection,
                    itemBuilder: (item, index) {
                      return CustomTitleChartPage(
                        title: item.controlMeasures ?? '',
                        titleRightWidget: _getResult('${item.controllable}'),
                        child: _itemWidget(item, controller),
                      );
                    },
                  ))
                ],
              ),
            ),
            // endDrawer: CustomEndDrawer(
            //   centerContext: Column(
            //     children: [
            //       _selectType(
            //           '清单内容',
            //           input: Container(
            //               height: 38.w,
            //               padding: EdgeInsets.symmetric(horizontal: 12.w),
            //               alignment: Alignment.center,
            //               decoration: BoxDecoration(
            //                   color: Colors.white,
            //                   border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
            //                   borderRadius: BorderRadius.all(Radius.circular(8.w))),
            //               child: Row(
            //                 children: [
            //                   Expanded(
            //                       child: Obx(() => TextField(
            //                           keyboardType: TextInputType.text,
            //                           textInputAction: TextInputAction.done,
            //                           controller:
            //                               TextEditingController(text: controller.searchX.value.isNotEmpty ? controller.searchX.value : controller.searchSelect),
            //                           onChanged: (value) {
            //                             controller.searchSelect = value;
            //                           },
            //                           style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
            //                           decoration: InputDecoration(
            //                             counterText: '',
            //                             //防止适配不居中
            //                             isCollapsed: true,
            //                             border: const OutlineInputBorder(borderSide: BorderSide.none),
            //                             contentPadding: EdgeInsets.only(left: 8.w),
            //                             hintText: '请输入搜索内容',
            //                             hintStyle: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
            //                           ))))
            //                 ],
            //               )),
            //           controller),
            //       _selectType('当前完成情况', controller),
            //       RegionalOrganizationTreePage(
            //         selectedEntity: controller.selectedEntity,
            //         onCallBack: (RiskTreeEntity entity) {
            //           controller.selectedEntity = entity;
            //         },
            //       ),
            //     ],
            //   ),
            //   reset: controller.reset,
            //   confirm: controller.confirm,
            // ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget(DailyInspectionChecklistController controller) {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: controller.searchX.value),
                    onChanged: (value) {
                      controller.searchX.value = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入搜索内容',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen(DailyInspectionChecklistController controller) {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  /// 落实结果  0待完成  1已完成
  Widget _getResult(String? i) {
    switch (i) {
      case '1':
        return _title('已完成', Colors.lightGreen);
      case '0':
        return _title('待完成', Colors.redAccent);
      default:
        return _title('未知', Colors.red);
    }
  }

  Widget _title(String title, Color color) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 11.w),
      decoration: BoxDecoration(
          color: color,
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
            bottomRight: Radius.circular(10),
          )),
      child: Text(
        title,
        style: TextStyle(
          color: Colors.white,
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
        ),
      ),
    );
  }

  Widget _itemWidget(RiskManagementListEntity item, DailyInspectionChecklistController controller) {
    return Column(
      children: [
        Row(
          children: [
            Text(
              '日常巡检清单：',
              style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
            ),
            Expanded(
              child: Align(
                alignment: Alignment.topRight,
                child: Text(
                  item.controlMeasures ?? '',
                  style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
                ),
              ),
            ),
          ],
        ),
        AppGaps.vGap10,
        Row(
          children: [
            Text(
              '详情：',
              style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
            ),
            const Spacer(),
            Container(
              padding: EdgeInsets.only(left: 10.w, right: 2.w, top: 2.w, bottom: 2.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: const Color(0xFF5C6DFF).withOpacity(0.1),
              ),
              child: Row(
                children: [
                  Text(
                    '检查项清单',
                    style: TextStyle(
                      color: const Color(0xFF5C6DFF),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  SvgPicture.asset(
                    AppImages.icon_right_arrow,
                  ),
                  AppGaps.hGap2,
                ],
              ).clickOnce(() {
                controller.dailyChecklist(item);
              }),
            )
          ],
        ),
        AppGaps.vGap10,
        Row(
          children: [
            Text(
              '详情：',
              style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
            ),
            const Spacer(),
            Container(
              padding: EdgeInsets.only(left: 10.w, right: 2.w, top: 2.w, bottom: 2.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(5),
                color: const Color(0xFF5C6DFF).withOpacity(0.1),
              ),
              child: Row(
                children: [
                  Text(
                    '历史执行情况',
                    style: TextStyle(
                      color: const Color(0xFF5C6DFF),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  // Icon(
                  //   Icons.arrow_right,
                  //   color: Color(0xFF5C6DFF),
                  // )
                  SvgPicture.asset(
                    AppImages.icon_right_arrow,
                  ),
                  AppGaps.hGap2,
                ],
              ).clickOnce(() {
                controller.clickHistoricalExecutionStatus(item);
              }),
            )
          ],
        ),
      ],
    );
  }
}
